package top.xiaoqiannb.shapelessdao.procedures;

import top.xiaoqiannb.shapelessdao.network.ShapelessdaoModVariables;
import top.xiaoqiannb.shapelessdao.init.ShapelessdaoModItems;
import top.xiaoqiannb.shapelessdao.init.ShapelessdaoModBlocks;

import net.minecraft.world.level.block.state.properties.Property;
import net.minecraft.world.level.block.state.BlockState;
import net.minecraft.world.level.LevelAccessor;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.entity.Entity;
import net.minecraft.server.level.ServerPlayer;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.network.chat.Component;
import net.minecraft.core.BlockPos;
import net.minecraft.client.Minecraft;
import net.minecraft.advancements.AdvancementProgress;
import net.minecraft.advancements.Advancement;

import java.util.Map;

public class ShaoxiangwudaoProcedure {
	public static void execute(LevelAccessor world, double x, double y, double z, Entity entity) {
		if (entity == null)
			return;
		if (!((entity.getCapability(ShapelessdaoModVariables.PLAYER_VARIABLES_CAPABILITY, null).orElse(new ShapelessdaoModVariables.PlayerVariables())).Faith).equals("Dao")) {
			{
				String _setval = "Dao";
				entity.getCapability(ShapelessdaoModVariables.PLAYER_VARIABLES_CAPABILITY, null).ifPresent(capability -> {
					capability.Faith = _setval;
					capability.syncPlayerVariables(entity);
				});
			}
			if (world.isClientSide())
				Minecraft.getInstance().gameRenderer.displayItemActivation(new ItemStack(ShapelessdaoModItems.DAO.get()));
			if (entity instanceof ServerPlayer _player) {
				Advancement _adv = _player.server.getAdvancements().getAdvancement(new ResourceLocation("shapelessdao:shapelessdao"));
				AdvancementProgress _ap = _player.getAdvancements().getOrStartProgress(_adv);
				if (!_ap.isDone()) {
					for (String criteria : _ap.getRemainingCriteria())
						_player.getAdvancements().award(_adv, criteria);
				}
			}
		} else {
			if (entity instanceof Player _player && !_player.level().isClientSide())
				_player.displayClientMessage(Component.literal("\u5927\u9053\u65E0\u5F62"), false);
			KaiGuangProcedure.execute(entity);
			if (entity instanceof Player _player && !_player.level().isClientSide())
				_player.displayClientMessage(Component.literal("\u5B55\u80B2\u5929\u5730"), false);
		}
		{
			BlockPos _bp = BlockPos.containing(x, y, z);
			BlockState _bs = ShapelessdaoModBlocks.INCENSE_BURNER.get().defaultBlockState();
			BlockState _bso = world.getBlockState(_bp);
			for (Map.Entry<Property<?>, Comparable<?>> entry : _bso.getValues().entrySet()) {
				Property _property = _bs.getBlock().getStateDefinition().getProperty(entry.getKey().getName());
				if (_property != null && _bs.getValue(_property) != null)
					try {
						_bs = _bs.setValue(_property, (Comparable) entry.getValue());
					} catch (Exception e) {
					}
			}
			world.setBlock(_bp, _bs, 3);
		}
		if (entity instanceof Player _player)
			_player.closeContainer();
	}
}
